package CheckersApplet;

import java.awt.*;
import java.applet.Applet;
import java.lang.*;
import java.util.ArrayList;

class Move {
	public int from;
	public int to;

	public Move(int from, int to) {
		this.from = from;
		this.to = to;
	}

	public String toString() {
		return String.valueOf(from) + " " + String.valueOf(to);
	}

	static String ArrayListToString(ArrayList<Move> moves) {

		String ret = "";

		if (moves == null || moves.isEmpty())
			return ret;
		
		for (Move m : moves) {
			ret += m.toString() + "\n";
		}

		return ret;
	}

	static Move RevertMove(Move toRevert) {
		return new Move(34 - toRevert.from, 34 - toRevert.to);
	}

	static ArrayList<Move> StringToArrayList(String toParse) {
		ArrayList<Move> ret = new ArrayList<Move>();
		String[] movesTab = toParse.split("\n");
		try {
			for (String str : movesTab) {
				int from = Integer.valueOf(str.split(" ")[0]);
				int to = Integer.valueOf(str.split(" ")[1]);			
				ret.add(new Move(from, to));
			}
		} catch (NumberFormatException e) {
			System.out.println(toParse);
			throw e;
		}
		return ret;
	}
}

class BoardGrid {
	public int xpos;
	public int ypos;
	public int xwidth;
	public int ywidth;
}

// Class Checkers
public class Checkers extends Applet {
	final int MenuBarHeight = 0;
	final int maxToughnessForUndo = 2;

	// Component Declaration
	public CheckersPanel checkersPanel;
	public Choice toughChoice;
	public Label toughLabel;
	public CheckboxGroup CheckboxGrpInCheckers;
	public Checkbox redRadio;
	public Checkbox blackRadio;
	public Button newGameButton;
	public Label colorLabel;
	public Button undoButton;
	public Label Title;
	public Image checkersLogo;
	public Label nameLabel;
	// End of Component Declaration

	private boolean infoShown = false;

	// init()
	public void init() {
		// Frame Initialization
		setForeground(Color.black);
		setBackground(Color.lightGray);
		setFont(new Font("Dialog", Font.BOLD, 12));
		setLayout(null);
		// End of Frame Initialization

		// Component Initialization
		checkersPanel = new CheckersPanel();
		toughChoice = new Choice();
		toughChoice.addItem("Wimp");
		toughChoice.addItem("Easy");
		toughChoice.addItem("Normal");
		toughChoice.addItem("Hard");
		toughChoice.addItem("Genius");
		toughChoice.setFont(new Font("Helvetica", Font.BOLD, 12));
		toughChoice.select(2);
		toughLabel = new Label("Toughness", Label.RIGHT);
		toughLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
		CheckboxGrpInCheckers = new CheckboxGroup(); // CheckboxGroup in
														// Checkers
		redRadio = new Checkbox("Red", CheckboxGrpInCheckers, false);
		redRadio.setBackground(Color.lightGray);
		redRadio.setFont(new Font("Dialog", Font.BOLD, 12));
		blackRadio = new Checkbox("Black", CheckboxGrpInCheckers, true);
		blackRadio.setBackground(Color.lightGray);
		blackRadio.setFont(new Font("Dialog", Font.BOLD, 12));
		newGameButton = new Button("New Game");
		newGameButton.setFont(new Font("Dialog", Font.BOLD, 14));
		colorLabel = new Label("Color", Label.RIGHT);
		colorLabel.setFont(new Font("Dialog", Font.BOLD, 12));
		undoButton = new Button("Undo");
		undoButton.setFont(new Font("Dialog", Font.BOLD, 12));
		Title = new Label("Java Checkers", Label.LEFT);
		Title.setFont(new Font("Dialog", Font.BOLD + Font.ITALIC, 12));
		nameLabel = new Label("by Bill Bereza", Label.LEFT);
		nameLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
		// End of Component Initialization
		// Add()s
		add(nameLabel);
		add(Title);
		add(undoButton);
		add(colorLabel);
		add(newGameButton);
		add(blackRadio);
		add(redRadio);
		add(toughLabel);
		add(toughChoice);
		add(checkersPanel);
		// End of Add()s

		InitialPositionSet();
	} // End of init()

	public ArrayList<Move> makeMove(int from, int to) {
		return checkersPanel.makeMove(from, to);
	}

	// start()
	public void start() {
		// checkersPanel.newGame();
	} // End of start()

	// stop()
	public void stop() {
	} // End of stop()

	// destroy()
	public void destroy() {
	} // End of destroy()

	public void paint(Graphics g) {
		// paint()
		// g.drawImage(checkersLogo, 365,413,35,46, this);
		// End of paint()
	}

	public void InitialPositionSet() {
		// InitialPositionSet()
		resize(410, 505);
		checkersPanel.reshape(5, 5 + MenuBarHeight, 400, 400);
		toughChoice.reshape(95, 413 + MenuBarHeight, 106, 27);
		toughLabel.reshape(2, 417 + MenuBarHeight, 88, 19);
		redRadio.reshape(95, 448 + MenuBarHeight, 59, 16);
		blackRadio.reshape(95, 466 + MenuBarHeight, 67, 16);
		newGameButton.reshape(284, 465 + MenuBarHeight, 116, 34);
		colorLabel.reshape(46, 453 + MenuBarHeight, 43, 19);
		undoButton.reshape(178, 465 + MenuBarHeight, 99, 34);

		Title.reshape(212, 417 + MenuBarHeight, 119, 19);
		nameLabel.reshape(213, 436 + MenuBarHeight, 99, 19);
		// End of InitialPositionSet()
		// checkersInfo.show();
		// checkersInfo.InitialPositionSet();
		// checkersPanel.makeMove(25, 21);
	}

	public boolean handleEvent(Event evt) {
		// handleEvent()
		if (evt.id == Event.WINDOW_DESTROY && evt.target == this)
			Checkers_WindowDestroy(evt.target);
		else if (evt.id == Event.ACTION_EVENT && evt.target == newGameButton)
			newGameButton_Action(evt.target);
		else if (evt.id == Event.ACTION_EVENT && evt.target == redRadio)
			redRadio_Action(evt.target);
		else if (evt.id == Event.ACTION_EVENT && evt.target == blackRadio)
			blackRadio_Action(evt.target);
		else if (evt.id == Event.ACTION_EVENT && evt.target == toughChoice)
			toughChoice_Action(evt.target);
		else if (evt.id == Event.ACTION_EVENT && evt.target == undoButton)
			undoButton_Action(evt.target);
		// End of handleEvent()

		return super.handleEvent(evt);
	}

	// Event Handling Routines
	public void Checkers_WindowDestroy(Object target) {
		System.exit(0);
	}

	public void newGameButton_Action(Object target) {
		checkersPanel.newGame();
	}

	public void redRadio_Action(Object target) {
		checkersPanel.setColor("red");
	}

	public void blackRadio_Action(Object target) {
		checkersPanel.setColor("black");
	}

	public void toughChoice_Action(Object target) {
		Choice myChoice = (Choice) target;
		int selected = myChoice.getSelectedIndex();

		if (selected > maxToughnessForUndo) {
			undoButton.hide();
			undoButton.disable();
		} else {
			undoButton.enable();
			undoButton.show();
		}
		// myErr(target);
		checkersPanel.setToughness(selected);
	}

	public void undoButton_Action(Object target) {
		checkersPanel.undo();
	}

	// End of Event Handling Routines

	public String getAppletInfo() {
		return "Java Checkers Copyright (c) 1997 by Bill Bereza\nAll Rights Reserved.\nJavaCheckers comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it\nunder certain conditions.";
	}

} // End of Class Checkers

